﻿

Public Class TiecCuoi_BLL
    'Them tiec cuoi
    Public Sub Them(ByVal tc As TiecCuoi)
        Dim db As New QLTCDataContext()
        db.TiecCuois.InsertOnSubmit(tc)
        db.SubmitChanges()

    End Sub

    Public Sub Sua(ByVal tc As TiecCuoi)
        Dim db As New QLTCDataContext()
        Dim query = From _tc In db.TiecCuois
                    Where _tc.ID = tc.ID
                    Select _tc

        For Each _tc In query
            _tc.TenChuRe = tc.TenChuRe
            _tc.TenCoDau = tc.TenCoDau
            _tc.DienThoai = tc.DienThoai
            _tc.Ngay = tc.Ngay
            _tc.Ca = tc.Ca
            _tc.Sanh = tc.Sanh
            _tc.TienDatCoc = tc.TienDatCoc
            _tc.SoLuongBan = tc.SoLuongBan
            _tc.SoBanDuTru = tc.SoBanDuTru
        Next
        db.SubmitChanges()
    End Sub

    Public Function Lay() As List(Of TiecCuoi)
        Dim db As New QLTCDataContext()
        Dim query = From tc In db.TiecCuois
             Select tc
        Return query.ToList()
    End Function


    Public Function LayMa() As Integer
        Dim db As New QLTCDataContext()
        Dim query = (From matieccuoi In db.TiecCuois Select (matieccuoi.ID)).Max()
        Return query.ToString()
    End Function

    Public Function LaySanh(ByVal ngay As DateTime, ByVal ca As String) As IList
        'lay ra tat ca cac sanh
        Dim sanhbus As New Sanh_BLL()
        Dim dssanh As IList = sanhbus.LayDanhSach()

        'lay ra cac sanh da duoc dat trong ngay do va trong ca do
        Dim db As New QLTCDataContext
        Dim sanhduocdat = From tieccuoi In db.TiecCuois
                   Where tieccuoi.Ngay = ngay.Date And tieccuoi.Ca = ca
                   Select tieccuoi.Sanh

        Dim query = From ds In dssanh
                From sddat In sanhduocdat
                Where ds.id <> sddat
                Distinct Select ds
        If sanhduocdat.Count = 0 Then
            Return dssanh
        End If
        Return query.ToList()
    End Function


End Class
